﻿using ELS.Models;
using ELS.Services;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using System;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ELS.Components.Pages
{
	public partial class AnswerPage
	{

		bool isShowInitial = true;
		bool isShowAnswer = false;

		Question currentQuestion;

		List<Question> questionList;

		QuestionService _questionService = new QuestionService();
		[Inject] IDialogService DialogService { get; set; }

		private int selectedAnswer;

		public int SelectedAnswer
		{
			get { return selectedAnswer; }
			set { selectedAnswer = value; OnSelectedAnswerChanged(); }
		}

		Random random = new Random();
		public async void StartAnswer()
		{
			isShowInitial = false;
			isShowAnswer = false;
			SelectedAnswer = 0;
			var result = await _questionService.GetAllQuestions();
			questionList = result.OrderBy(x => random.Next()).Take(5).ToList();
			if (questionList.Any())
			{
				for (int i = 0; i < questionList.Count; i++)
				{
					questionList[i].Index = i + 1;
					questionList[i].UserAnswerIndex = 0;
				}

				currentQuestion = questionList.FirstOrDefault();
			}

			StateHasChanged();
		}

		public void OnSelectedAnswerChanged()
		{
			if (currentQuestion == null)
			{
				return;
			}
			currentQuestion.UserAnswerIndex = selectedAnswer;
		}

		public void PreviousQuestion()
		{
			var index = questionList.IndexOf(currentQuestion);
			if (index > 0)
			{
				currentQuestion = questionList[index - 1];
			}
			else
			{
				return;
			}

			selectedAnswer = currentQuestion.UserAnswerIndex;
			StateHasChanged();
		}

		public async void NextQuestion()
		{
			var index = questionList.IndexOf(currentQuestion);
			if (index < questionList.Count - 1)
			{
				currentQuestion = questionList[index + 1];
			}
			else
			{
				var dialog = await DialogService.ShowAsync<SimpleDialog>($"已答题完毕，是否提交?");
				var result = await dialog.Result;

				if (result.Canceled)
				{
					return;
				}
				//TODO: 提交答卷
				SubmitAnswer();
			}
			selectedAnswer = currentQuestion.UserAnswerIndex;
			StateHasChanged();
		}

		public double[] data = { 25, 77, 28, 5 };
		public string[] labels = { "Oil", "Coal", "Gas", "Biomass" };

		public async void SubmitAnswer()
		{
			var currentCount = questionList.Count(q => q.UserAnswerIndex == q.CurentAnswerIndex);
			var totalCount = questionList.Count;

			data = [currentCount, totalCount - currentCount];
			labels = ["正确", "错误"];

			var score = (currentCount / (double)totalCount) * 100;
			var dialog = await DialogService.ShowAsync<SimpleDialog>($"答对{currentCount}题，共{totalCount}题，正确率：{score:f2}%，是否查看正确答案?");
			var result = await dialog.Result;
			if (result.Canceled)
			{
				questionList.Clear();
				currentQuestion = null;
				selectedAnswer = 0;
				isShowInitial = true;
			}
			else
			{
				//TODO: 查看正确答案
				isShowAnswer = true;
			}

			StateHasChanged();
		}

		string ConvertAnswer(int index)
		{
			switch (index)
			{
				case 1:
					return "A";
				case 2:
					return "B";
				case 3:
					return "C";
				case 4:
					return "D";
				default:
					return "";
			}
		}
	}
}
